﻿Imports System.Data

Partial Class private_secure_members_payment_BuyCredit
    Inherits System.Web.UI.Page
    Dim u As MembershipUser


    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        u = Membership.GetUser(HttpContext.Current.User.Identity.Name)
        If Not Page.IsPostBack Then
            ListView1.DataBind()
        End If
        ButtonsChange()
        If Not Session("BuyDT") Is Nothing Then
            CalculatePrice()
        End If

    End Sub


    Protected Sub ListView1_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListView1.ItemCommand
        If e.CommandName.ToString() = "SelectItem" Then
            If Session("BuyDT") Is Nothing Then
                Dim table As DataTable = GetTable()
                table.Rows.Add(CInt(e.CommandArgument.ToString()))
                Dim distincttable As DataTable = table.DefaultView.ToTable(True, "ItemId")
                Session("BuyDT") = distincttable
            Else
                Dim table As DataTable = CType(Session("BuyDT"), DataTable)

                If Not table Is Nothing Then
                    table.Rows.Add(CInt(e.CommandArgument.ToString()))
                    Session.Remove("BuyDT")
                    'DataTable filterTable = yourDataTable.DefaultView.ToTable("TargetTable", true, "Consumer", "No", "Date")
                    Dim distincttable As DataTable = table.DefaultView.ToTable(True, "ItemId")
                    Session("BuyDT") = distincttable
                End If

            End If
        ElseIf e.CommandName.ToString() = "UnselectItem" Then
            If Session("BuyDT") Is Nothing Then
            Else
                Dim table As DataTable = CType(Session("BuyDT"), DataTable)
                For Each dRow As DataRow In table.Rows
                    Dim m = dRow("ItemId").ToString()
                    If dRow("ItemId").ToString() = e.CommandArgument.ToString() Then
                        dRow.Delete()
                        CalculatePrice()
                        Return
                    End If

                Next
            End If

        End If
        CalculatePrice()

    End Sub
    'Sub BindGrid()
    '    Dim table As DataTable = CType(Session("BuyDT"), DataTable)
    '    GridView1.DataSource = table
    '    GridView1.DataBind()

    'End Sub
    Sub CalculatePrice()
        Dim dec As Decimal
        Dim ProductsTA As New LogginsLogDSTableAdapters.usp_ShowCreditsByEntityTableAdapter()
        Dim ProductsDT As LogginsLogDS.usp_ShowCreditsByEntityDataTable
        If Not Session("SelectedEntityID") Is Nothing Then
            Dim x As Integer = Session("SelectedEntityID")
            ProductsDT = ProductsTA.GetData(x, dec)
        Else
            ProductsDT = ProductsTA.GetData(23, dec)
        End If
        lblTotal.Text = 0
        If Not Session("BuyDT") Is Nothing Then
            Dim table As DataTable = CType(Session("BuyDT"), DataTable)
            For Each dRow As DataRow In table.Rows
                Dim price = (From p In ProductsDT.AsEnumerable() Where p.ItemId = dRow("ItemId").ToString() Select p.Total).FirstOrDefault()
                lblTotal.Text = Convert.ToDecimal(lblTotal.Text) + price
            Next
        End If
        ButtonsChange()
    End Sub

    Function GetTable() As DataTable
        ' Create new DataTable instance.
        Dim table As New DataTable
        ' Create four typed columns in the DataTable.
        'table.Columns.Add("Id", GetType(Integer))
        table.Columns.Add("ItemId", GetType(Integer))
        'table.Columns.Add("UnitPrice", GetType(String))
        ' Add five rows with those columns filled in the DataTable.
        'table.Rows.Add(25, "Indocin", "David", DateTime.Now)
        'table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
        'table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
        'table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
        'table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
        Return table
    End Function

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Session.Remove("BuyDT")
        CalculatePrice()
    End Sub

    Protected Sub ObjectDataSource1_Selecting(sender As Object, e As ObjectDataSourceSelectingEventArgs) Handles ObjectDataSource1.Selecting

        If Not Session("SelectedEntityID") Is Nothing Then
            Dim x = Session("SelectedEntityID").ToString()
            e.InputParameters("EntityId") = Session("SelectedEntityID")
        Else
            Response.Redirect("~/private/secure_members/payment/MyCredit.aspx")
        End If
    End Sub

    Sub ButtonsChange()
        For Each listitem As ListViewDataItem In ListView1.Items
            Dim lbox As Label = TryCast(listitem.FindControl("ItemIdLabel"), Label)
            If lbox IsNot Nothing Then
                If Not Session("BuyDT") Is Nothing Then
                    Dim table As DataTable = CType(Session("BuyDT"), DataTable)
                    Dim check = From t In table.AsEnumerable() Where t.Field(Of Integer)("ItemId") = CInt(lbox.Text) Select t
                    Dim abutton As LinkButton = TryCast(listitem.FindControl("SelectButton"), LinkButton)
                    Dim ubutton As LinkButton = TryCast(listitem.FindControl("UnselectButton"), LinkButton)

                    If check.Count > 0 Then
                        abutton.Visible = False
                        ubutton.Visible = True
                    Else
                        abutton.Visible = True
                        ubutton.Visible = False
                    End If

                Else
                    Dim abutton As LinkButton = TryCast(listitem.FindControl("SelectButton"), LinkButton)
                    Dim ubutton As LinkButton = TryCast(listitem.FindControl("UnselectButton"), LinkButton)

                    abutton.Visible = True
                    ubutton.Visible = False

                End If
            End If
        Next
    End Sub


    Protected Sub ButtonContinue_Click(sender As Object, e As EventArgs) Handles ButtonContinue.Click
        If Not lblTotal.Text = 0 Then
            Response.Redirect("~/private/secure_members/payment/ConfirmOrder.aspx")
        Else
            MsgBox("Please select one or many items.")
        End If

    End Sub

    Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("~/private/secure_members/payment/MyCredit.aspx")
    End Sub
End Class
